package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatCoupon;
import com.happy.cat.project.system.service.ICatCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 优惠券管理Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/coupon" )
@Api("优惠券管理 API")
public class CatCouponController extends BaseController {

    private final ICatCouponService iCatCouponService;

    /**
     * 查询优惠券管理列表
     */
    @ApiOperation(value = "查询优惠券管理列表",notes = "查询优惠券管理列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatCoupon catCoupon)
    {
        startPage();
        LambdaQueryWrapper<CatCoupon> lqw = new LambdaQueryWrapper<CatCoupon>();
        if (StringUtils.isNotBlank(catCoupon.getName())){
            lqw.like(CatCoupon::getName ,catCoupon.getName());
        }
        if (catCoupon.getCouponType() != null){
            lqw.eq(CatCoupon::getCouponType ,catCoupon.getCouponType());
        }
        if (catCoupon.getCouponNumber() != null){
            lqw.eq(CatCoupon::getCouponNumber ,catCoupon.getCouponNumber());
        }
        if (catCoupon.getCouponMoney() != null){
            lqw.eq(CatCoupon::getCouponMoney ,catCoupon.getCouponMoney());
        }
        if (catCoupon.getSill() != null){
            lqw.eq(CatCoupon::getSill ,catCoupon.getSill());
        }
        if (catCoupon.getSillMoney() != null){
            lqw.eq(CatCoupon::getSillMoney ,catCoupon.getSillMoney());
        }
        if (catCoupon.getExpirationType() != null){
            lqw.eq(CatCoupon::getExpirationType ,catCoupon.getExpirationType());
        }
        if (StringUtils.isNotBlank(catCoupon.getExpirationVal())){
            lqw.eq(CatCoupon::getExpirationVal ,catCoupon.getExpirationVal());
        }
        if (catCoupon.getSkillType() != null){
            lqw.eq(CatCoupon::getSkillType ,catCoupon.getSkillType());
        }
        if (StringUtils.isNotBlank(catCoupon.getRemarks())){
            lqw.eq(CatCoupon::getRemarks ,catCoupon.getRemarks());
        }
        if (catCoupon.getCreateDate() != null){
            lqw.eq(CatCoupon::getCreateDate ,catCoupon.getCreateDate());
        }
        if (catCoupon.getStatus() != null){
            lqw.eq(CatCoupon::getStatus ,catCoupon.getStatus());
        }
        List<CatCoupon> list = iCatCouponService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出优惠券管理列表
     */
    @ApiOperation(value = "导出优惠券管理列表",notes = "导出优惠券管理列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:coupon:export')" )
    @Log(title = "优惠券管理" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatCoupon catCoupon) {
        LambdaQueryWrapper<CatCoupon> lqw = new LambdaQueryWrapper<CatCoupon>(catCoupon);
        List<CatCoupon> list = iCatCouponService.list(lqw);
        ExcelUtil<CatCoupon> util = new ExcelUtil<CatCoupon>(CatCoupon. class);
        return util.exportExcel(list, "coupon" );
    }

    /**
     * 获取优惠券管理详细信息
     */
    @ApiOperation(value = "获取优惠券管理详细信息",notes = "获取优惠券管理详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:coupon:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatCouponService.getById(id));
    }

    /**
     * 新增优惠券管理
     */
    @ApiOperation(value = "新增优惠券管理",notes = "新增优惠券管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:coupon:add')" )
    @Log(title = "优惠券管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatCoupon catCoupon) {
        catCoupon.setCreateDate(new Date());
        catCoupon.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatCouponService.save(catCoupon) ? 1 : 0);
    }

    /**
     * 修改优惠券管理
     */
    @ApiOperation(value = "修改优惠券管理",notes = "修改优惠券管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:coupon:edit')" )
    @Log(title = "优惠券管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatCoupon catCoupon) {
        catCoupon.setCreateDate(new Date());
        catCoupon.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatCouponService.updateById(catCoupon) ? 1 : 0);
    }

    /**
     * 删除优惠券管理
     */
    @ApiOperation(value = "删除优惠券管理",notes = "删除优惠券管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:coupon:remove')" )
    @Log(title = "优惠券管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatCouponService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
